package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.Event;

import com.example.entity.Result;
import com.example.service.EventService;
import com.example.utils.ResultUtil;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 */

@RestController
@RequestMapping("/event")
public class EventController {

    @Autowired
    EventService eventService;

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Event> eventList = eventService.selectAll();
        return ResultUtil.success(eventList);
    }

    @GetMapping("/selectOne")
    public Result selectOne(Long id) {
        Event event = eventService.selectOne(id);
        return ResultUtil.success(event);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody  Event e) {
        JSONObject jsonObject = eventService.insert(e);
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            Event eventNode = (Event) jsonObject.get("data");
            return ResultUtil.success(eventNode);
        }
        return ResultUtil.error(500, msg);
    }


    @PostMapping("/update")
    public Result update(@RequestBody  Event e) {
        JSONObject jsonObject = eventService.update(e);
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            Event eventNode = (Event) jsonObject.get("data");
            return ResultUtil.success(eventNode);
        }
        return ResultUtil.error(500, msg);
    }


    @PostMapping("deleteById") // 删除事件
    public Result deleteById(@RequestBody Event e) {
        JSONObject jsonObject = eventService.deleteById(e);
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            Event eventNode = (Event) jsonObject.get("data");
            return ResultUtil.success(eventNode);
        }
        return ResultUtil.error(500, msg);
    }
}
